package com.example.fw;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


public class GroupHelper extends  HelperWithGetDriverBase {

	public static Groups groupModel;
	private int lastGroupId;
	
	public GroupHelper(ApplicationManager manager){
        super(manager);
        lastGroupId = Integer.parseInt(manager.getHibernateHelper().gerLastGroupId());
	}
	
	public Groups getUIGroups() {
		List<GroupObject> set = new ArrayList<GroupObject>();
		manager.getNavigationHelper().goToGroupListPage();
		WebElement form = findElements(By. tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			GroupObject group = workWithStringAndCreateNewGroupInstance(checkbox);
			set.add(group);
		}
		return new Groups(set);
	}
	
	public Groups getGroups() {
		if (groupModel == null){
			groupModel = manager.getHibernateHelper().getGroups();
		}
		return groupModel;
	}

	public GroupObject getGroupByNumber(int groupNumber) {
		manager.getNavigationHelper().goToGroupListPage();
		WebElement form = findElements(By.tagName("form")).get(1);
		WebElement checkbox = form.findElement(By.name("selected[]"));
		GroupObject group = workWithStringAndCreateNewGroupInstance(checkbox);
		return group;   
	}
	
	private GroupObject workWithStringAndCreateNewGroupInstance(
			WebElement checkbox) {
		String title = checkbox.getAttribute("title");
		Pattern p = Pattern.compile(".*\\((.*)\\)");
		Matcher m = p.matcher(title);
		if (m.matches()){
			title = m.group(1);
		}
		GroupObject group = new GroupObject()
		 .setId(checkbox.getAttribute("value"))
		 .setName(title);
		return group;
	}
	
	public void createGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupCreation();	
		fillGroupForm(groupObject);		
		submitGroupCreation();
		returnToGroupListPage();
		lastGroupId++;
		groupObject.setId("" + lastGroupId);
		groupModel = groupModel.withAdded(groupObject);
	}

	private void returnToGroupListPage() {
		click(By.linkText("group page"));
	}

	private void submitGroupCreation() {
		click(By.name("submit"));
	}

	private void fillGroupForm(GroupObject groupObject) {
		type(By.name("group_name"), groupObject.name);
		type(By.name("group_header"), groupObject.header);
		type(By.name("group_footer"), groupObject.footer);
	}
		
	private void initGroupCreation() {
		click(By.name("new"));
	}

	public void deleteGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		checkGroup(groupObject);
		click(By.name("delete"));
		returnToGroupListPage();
		groupModel = groupModel.without(groupObject);
	}
	
	public void modifyGroup(GroupObject oldGroupObject, GroupObject newGroupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupModification(oldGroupObject);	
		fillGroupForm(newGroupObject);		
		submitGroupModification();
		returnToGroupListPage();
		groupModel = groupModel.without(oldGroupObject).withAdded(newGroupObject);
	}

	private void initGroupModification(GroupObject groupObject) {
		checkGroup(groupObject);
		click(By.name("edit"));
	}
		
	private void checkGroup(GroupObject groupObject) {
		click(By.xpath("//input[@value='" + groupObject.id + "']"));
	}
	
	private void submitGroupModification() {
		click(By.xpath("/html/body/div/div[4]/form/input[3]"));
	}
}